<?php

namespace App\Console\Commands\Test\RabbmitMQ\Direct;

use Illuminate\Console\Command;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

class Publish extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'rabbitMQ:publish-direct {type?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'rabbitMQ public direct demo';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try {

            $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');

            $channel = $connection->channel();

            // 创建交换机logs
            $channel->exchange_declare('direct_logs', 'direct', false, false, false);

            $severity = $this->argument('type') ?: 'info';

            echo  "[rabbitMQ publish start]发布消息中\n" ;

            $msg = new AMQPMessage('test direct', ['delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT]);

            $channel->basic_publish($msg, 'direct_logs', $severity);

            echo  "[rabbitMQ publish end]发布消息结束\n" ;
            //

        } catch (\Exception $e) {
            $this->info($e->getMessage());
        }

    }
}
